package com.singzi.N1300;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class T1206 {
    public static void main(String[] args) {
        int [][]grid = {
                {3,8,1,9},{19,7,2,5},{4,6,11,10},{12,0,21,13}

        };
        Solution1206 solution1206 = new Solution1206();
        System.out.println(solution1206.shiftGrid(grid, 4));

    }
}
class Solution1206 {

    public List<List<Integer>> shiftGrid(int[][] grid, int k) {
        int m = grid.length;
        int n = grid[0].length;

        for(int i = 0;i < k;i++){
            grid =  shift(grid);
        }
        List<List<Integer>> ans = new ArrayList<>();
        for (int[] ints : grid) {
            List<Integer> temp1 = new ArrayList<>();
            for (int j = 0; j < n; j++) {
                temp1.add(ints[j]);
            }
            ans.add(new ArrayList<>(temp1));
        }
        return ans;
    }
    public int[][] shift(int[][] grid){
        int [][]temp = new int[grid.length][grid[0].length];
        for(int i = 0;i < grid[0].length-1;i++){
            for(int j = 0;j < grid.length;j++){
                temp[j][i+1] = grid[j][i];
            }
        }
        temp[0][0] = grid[grid.length-1][grid[0].length-1];
        for(int i = 0;i < grid.length-1;i++){
            temp[i+1][0] = grid[i][grid[0].length-1];
        }
        return temp;
    }
}
